package com.honey.mamabike.user.controller;

import com.honey.mamabike.common.constants.Constants;
import com.honey.mamabike.common.exception.MaMaBikeException;
import com.honey.mamabike.common.resp.ApiResult;
import com.honey.mamabike.common.resp.BaseController;
import com.honey.mamabike.user.entity.LoginInfo;
import com.honey.mamabike.user.entity.User;
import com.honey.mamabike.user.entity.UserElement;
import com.honey.mamabike.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 *
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController extends BaseController {

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    /**
     * 用户登录注册
     * @param loginInfo
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResult<String> login(@RequestBody LoginInfo loginInfo){

        ApiResult<String> resp = new ApiResult<>();
        try {
            String data = loginInfo.getData();
            String key = loginInfo.getKey();
            if(StringUtils.isBlank(data) || StringUtils.isBlank(key)){
                throw new MaMaBikeException("参数校验失败");
            }
            String token = userService.login(data,key);
            resp.setData(token);

        }catch (MaMaBikeException e){
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage(e.getMessage());
        }catch (Exception e){
            log.error("Fail to login",e);
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage("内部错误");
        }

        return resp;
    }

    /**
     * 修改用户昵称
     * @param user
     * @return
     */
    @RequestMapping("/modifyNickName")
    public ApiResult modifyNickName(@RequestBody User user){
        ApiResult resp = new ApiResult();

        try {
          UserElement ue =  getCurrentUser();
          user.setId(ue.getUserId());
            userService.modifyNickName(user);

        }catch (MaMaBikeException e){
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage(e.getMessage());
        }catch (Exception e){
            log.error("Fail to login",e);
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage("内部错误");
        }

        return null;

    }




    @RequestMapping("/sendVercode")
    public ApiResult<String> sendVercode(@RequestBody User user, HttpServletRequest request){

      /*  ApiResult<String> resp = new ApiResult<>();
        try {
            userService.sendVercode(User.getMobile(),getIpFromRequest(request));

        }catch (MaMaBikeException e){
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage(e.getMessage());
        }catch (Exception e){
            log.error("Fail to login",e);
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage("内部错误");
        }
*/
        return null;
    }








}
